<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
namespace Library\Dal\Driver\Mysql;

class Adapter implements \Library\Dal\AdapterInterface
{
    protected $_resource;
    protected $_settings;
    
    public $isConnected = false;
    
    public function __construct($settings)
    {
    	if (!is_array($settings))
		{
			$config = \Library\Application::getInstance()-&gt;config;
			
			$settings = array(
				'server' =&gt; $config['database'][$settings]['server'],
				'username' =&gt; $config['database'][$settings]['username'],
				'password' =&gt; $config['database'][$settings]['password'],
				'database' =&gt; $config['database'][$settings]['database']
			);
		}
		
        $this-&gt;_settings = $settings;
    }
    
    public function getConnection()
    {
        if ( ! $this-&gt;_resource instanceof \PDO )
        {
            $this-&gt;connect();
        }
        
        return $this-&gt;_resource;
    }
    
    public function connect()
    {
        $this-&gt;_resource = new \PDO( &quot;mysql:dbname=&quot; . $this-&gt;_settings[&quot;database&quot;] . &quot;;host=&quot; . $this-&gt;_settings[&quot;server&quot;], 
             $this-&gt;_settings[&quot;username&quot;], 
             $this-&gt;_settings[&quot;password&quot;], 
             array(
                 \PDO::ATTR_PERSISTENT =&gt; true,
                 \PDO::ATTR_ERRMODE =&gt; \PDO::ERRMODE_EXCEPTION
             ) 
         );
            
        if ( $this-&gt;_resource !== false )
        {
            $this-&gt;isConnected = true;
        }
    }
    
    public function getAttr($key)
    {
        return $this-&gt;_resource-&gt;getAttribute($key);
    }
    
    public function setAttr($key, $value)
    {
        return $this-&gt;_resource-&gt;setAttribute($key, $value);
    }
    
    public function prepare($query)
    {
        $st = $this-&gt;getConnection()-&gt;prepare($query);
		
        if ($query instanceof \Library\Dal\Query || $query instanceof \Library\Dal\Driver\Mysql\ActiveRecord)
		{
			foreach($query-&gt;getParameters() as $parameter)
	        {
	            $st-&gt;bindParam($parameter['0'], $parameter['1'], $parameter['2']);
	        }
		}
        
        return $st;
    }

    public function query($query)
    {
        $st = $this-&gt;prepare($query);
        
        $st-&gt;execute();
        
        return $st;
    }

    public function fetchAll($query)
    {
        return $this-&gt;Query($query)-&gt;fetchAll( \PDO::FETCH_ASSOC );
    }

    public function fetchRow($query)
    {
        return $this-&gt;Query($query)-&gt;fetch( \PDO::FETCH_ASSOC );
    }

    public function countRows($query)
    {
        
    }
    
    public function close()
    {
        $this-&gt;_resource = null;
    }
}</pre>
</body>
</html>
